package com.wang.controller;


import com.wang.base.http.RestResponse;
import com.wang.domain.User;
import com.wang.domain.enums.UserRoleEnum;
import com.wang.model.User.AddManagerVM;
import com.wang.model.User.UserConditionSelectVM;
import com.wang.model.User.UserSelectVM;
import com.wang.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author WangZai
 * @since 2023-03-25
 */
@CrossOrigin
@RestController
@RequestMapping("/td/api/admin")
@Api(tags = "管理员用户模块接口说明")
public class AdminUserController {
    private final ModelMapper modelMapper;
    private final UserService userService;


    private static final Logger log = LoggerFactory.getLogger(TongueDiagnosisController.class);

    @Autowired
    public AdminUserController(ModelMapper modelMapper, UserService userService) {
        this.modelMapper = modelMapper;
        this.userService = userService;
    }


    @PostMapping(value = "/selectUsers")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("查询用户接口说明")
    public RestResponse<Object> selectUsers(@RequestBody UserConditionSelectVM conditionModel) {
        List<User> users = userService.getUserByCondition(conditionModel.getUserId(), conditionModel.getUserName(), conditionModel.getRealName());
        List<UserSelectVM> userSelectVMList = new ArrayList<>();
        for (User user : users) {
            UserSelectVM userSelectVM = modelMapper.map(user, UserSelectVM.class);
            if (user.getRole() != null) {
                userSelectVM.setRole(user.getRole().getName());
            }
            if (user.getSex() != null) {
                userSelectVM.setSex(user.getSex().getName());
            }
            userSelectVMList.add(userSelectVM);
        }
        if (users.isEmpty()) {
            return RestResponse.failure("不存在符合条件的用户");
        }
        return RestResponse.ok(userSelectVMList);
    }


    @GetMapping(value = "/deleteUser/{userId}")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("删除用户接口说明")
    public RestResponse<Object> deleteUser(@PathVariable Integer userId) {
        if (userService.deleteUserById(userId)) {
            return RestResponse.ok("删除成功");
        } else {
            return RestResponse.failure("删除失败");
        }
    }

    @PostMapping(value = "/addManager")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("添加管理员接口说明")
    public RestResponse<Object> addManager(@RequestBody AddManagerVM model) {
        User user = modelMapper.map(model,User.class);
        user.setRole(UserRoleEnum.ADMIN);
        User user1 = userService.getUserByPhone(model.getPhone());
        User user2 = userService.getUserByUserName(model.getUserName());
        if (user1 != null && !user1.getUserId().equals(user.getUserId())) {
            return RestResponse.failure("信息重复,添加失败");
        }
        if (user2 != null && user2.getUserId().equals(user.getUserId())) {
            return RestResponse.failure("信息重复,添加失败");
        }
        userService.saveUser(user);
        return RestResponse.ok("添加管理员成功");
    }

}

